package com.globallogic.training.javase.university.subject;

import com.globallogic.training.javase.university.member.Group;
import com.globallogic.training.javase.university.member.Teacher;

/**
 * This class represents any kind of learning activities within university.
 * 
 * @author Ivan Moisiienko
 * @version 0.1 2013/04/12
 */
public abstract class Activity implements Comparable<Activity> {

	private Teacher teacher;
	
	private Group group;
	
	private int numberInPlan;
	
	private Subject subject;

	/**
	 * @param numberInPlan
	 *            should be positive
	 * @param load
	 *            should be positive
	 * @param teacher
	 *            Teacher argument cannot be null
	 * @param group
	 *            Group argument cannot be null
	 */
	public Activity(int numberInPlan, Teacher teacher, Group group,
			Subject subject) {

		if (numberInPlan <= 0) {
			throw new IllegalArgumentException(
					"Number in plan should be positive");
		}

		if (teacher == null) {
			throw new IllegalArgumentException("Teacher argument is null");
		}

		if (group == null) {
			throw new IllegalArgumentException("Group argument is null");
		}

		this.numberInPlan = numberInPlan;
		this.teacher = teacher;
		this.group = group;
		this.subject = subject;
	}

	public int getNumber() {
		return numberInPlan;
	}

	public Group getGroup() {
		return group;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public Subject getSubject() {
		return subject;
	}
	
	public String toString() {
		return "[" + teacher.toString() + ", " + group.toString() + "]";
	}

	@Override
	public int compareTo(Activity other) {
		String groupId = this.getGroup().getGroupId();
		String otherGroupId = other.getGroup().getGroupId();
		int groupCompare = groupId.compareTo(otherGroupId);
		int numberCompare = this.numberInPlan - other.numberInPlan;

		if (groupCompare > 0 || (groupCompare == 0 && numberCompare > 0)) {
			return 1;
		} else if (groupCompare < 0 || (groupCompare == 0 && numberCompare > 0)) {
			return -1;
		}
		return 0;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + getGroup().hashCode();
		result = prime * result + getTeacher().hashCode();
		return result;
	}

	@Override
	public boolean equals(Object otherObject) {
		if (this == otherObject) {
			return true;
		}
		if (otherObject == null) {
			return false;
		}
		if (getClass() != otherObject.getClass()) {
			return false;
		}

		Activity other = (Activity) otherObject;
		if (this.getGroup().equals(other.getGroup())
				&& this.getTeacher().equals(other.getTeacher())
				&& this.getNumber() == other.getNumber()) {
			return true;
		}
		return false;
	}

}
